layui.use(['element','form','jquery','tree'], function(){
    var form = layui.form,element = layui.element,tree = layui.tree,$=layui.jquery,delresourcesul;
   // var data = [{"resourcestext":"供应商中心"},{"resourcestext":"邮件"},{"resourcestext":"产品"},{"resourcestext":"文件"},{"resourcestext":"消息"}];
   var data=[];
    getData();
    init();
    function getData(){
        $.ajax({
            type : 'POST',
            url : 'resource/selectAll',
            async:false,
            success : function (da) {
                if(da.status==200 && da.data.length >0){
                    data=da.data;
                }
            },
            error: function(data) {
                layer.msg("初始化失败!");
            }
        });
    }
    //刷新
    function refresh(){
        getData();
        init();
    }
    //  提交
    form.on('submit(resourcesup-form-element)', function(data){
       /* $("#resourcId").val();
       layer.msg("修改提交"+JSON.stringify(data.field));
        return false;*/

        $.ajax({
            type : 'POST',
            url : 'resource/update',
            data:JSON.stringify(data.field),
            dataType:"json",
            contentType: 'application/json',
            async:false,
            success : function (data) {
                if (data.status == 200 && data.data==1){
                    layer.msg("更新成功！");
                    refresh();
                    clear();
                }else{
                    layer.msg("更新失败！");
                }
            },
            error: function(data) {
                layer.msg("更新失败！");
            }
        });

    });
    //保存
    form.on('submit(resources-form-element)', function(data){
       /* layer.msg("新增提交"+JSON.stringify(data.field));
        return false;*/

        $.ajax({
            type : 'POST',
            url : 'resource/insert',
            data:JSON.stringify(data.field),
            dataType:"json",
            contentType: 'application/json',
            async:false,
            success : function (data) {
                var obj = JSON.parse(data);
                var da=obj.data;
                if (obj.status == 200 && da.length > 0){
                    layer.msg("保存成功！");
                }else{
                    layer.msg("保存失败！");
                }
            },
            error: function(data) {
                layer.msg("保存失败！");
            }
        });
    });
    //初始化
    function init() {
    //动态生成element
    var resourcesulhtml = "";
    for (var i = 0;i<data.length;i++){
        /* var href="javascript:void(0)";
         if(data[i].url != null) href=data[i].url*/

        resourcesulhtml += '<li class="layui-nav-item" code="'+data[i].code+'" url="'+data[i].url +
            '"  name="'+data[i].name+'" id="'+data[i].id+'"><a href="javascript:;">'+data[i].name+'</a></li>';

    }
    $("#resourcesul").html(resourcesulhtml);
    $(document).on('click','li.layui-nav-item',function(data){
        var attrs=$(this)[0].attributes;//$(this).parent().context.attributes;
        var name="",code="",url="",resourcId="";
        for(var i=0;i<attrs.length;i++){
            if(attrs[i].name=="code"){
                code=attrs[i].value;
            }
            if(attrs[i].name=="name") {
                name=attrs[i].value;
            }
            if(attrs[i].name=="url") {
                url=attrs[i].value;
            }
            if(attrs[i].name=="id") {
                resourcId=attrs[i].value;
            }
        }
        $("input[name='name']")[0].value=name;
        $("input[name='code']")[0].value=code;
        $("input[name='url']")[0].value=url;
        $("#resourcId").val(resourcId);
        $("input[name='code']")[0].readOnly=true
    });
    element.render();
}


    //监听导航点击
    element.on('nav(resourcesul)', function(elem){
        //console.log(elem)
      // layer.msg(elem.text());
        delresourcesul = elem.text();
        $("#resourceselement").css("display","none");
        $("#resourcesupelement").css("display","block");
        return false;
    });
    //点击新增
    $("#newresources").on("click",function () {
        $("#resourcesupelement").css("display","none");
        $("#resourceselement").css("display","block");
        $("input[name='code']")[0].readOnly=false;
        clear();
    })
    //清空
    function clear(){
        $("input[name='name']")[0].value="";
        $("input[name='code']")[0].value="";
        $("input[name='url']")[0].value="";
        $("#resourcId").val("");
    }
    //点击删除
    $("#companylist").on("click",function () {
       // layer.msg("删除资源:"+delresourcesul);

        $.ajax({
            type : 'POST',
            url : 'resource/delete',
            data:{"id":$("#resourcId").val()},
            async:false,
            success : function (data) {
                if (data.status == 200 && data.data==1){
                    layer.msg("删除成功！");
                    refresh();
                    clear();
                }else{
                    layer.msg("删除失败！");
                }
            },
            error: function(data) {
                layer.msg("删除失败！");
            }
        });

    })
    $("#searchs").on("click",function () {
        var resourcessearch = $('#resourcessearch').val();
       // layer.msg("搜索"+resourcessearch);
        var attrs= $(this).parent()[0].nextElementSibling.childNodes;
        for(var i=0;i<attrs.length;i++){
            $(this).parent()[0].nextElementSibling.childNodes[i].className='layui-nav-item';
            clear();
        }
        for(var i=0;i<attrs.length;i++){
            var objs=attrs[i].attributes;
            var cun=0;
            for(var n=0;n < objs.length;n++){
                if(objs[n].name=="name" && objs[n].value.indexOf(resourcessearch) != -1){
                    $($(this).parent()[0].nextElementSibling.childNodes[i]).click();
                    //layui-this
                    $(this).parent()[0].nextElementSibling.childNodes[i].className='layui-nav-item layui-this';
                    cun++;
                    break;
                }
            }
            if(cun >0) break;
        }
        /*$($(this).parent()[0].nextElementSibling.childNodes[0]).click();
        $(this).parent()[0].nextElementSibling.childNodes[0].attributes[3].value*/
    })
});